// gjw/pages/jishi/youxuan.js

const app = getApp()
var dataheith = [0];
var activeIndex = 0;

Page({

  /**
   * 页面的初始数据
   */
  data: {
    page:1,
    menu:['推荐','全部','分类'],
    tabIndex:0,
    activeNav:0,
    lists: [],
    list: [],
    content: [],
    noData:true,
    activeIndex: 0,
    
    tab_config: {
      tabs: [],
      tab_left: 0,
    },
    scroll: "scroll"
    
  },

  // tabbar.js
  tabbar: function (e) {
    
    var that = this
    var url = e.currentTarget.dataset.url
    wx.reLaunch({
      url: url,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
    app.route(this)
		var siteUrl = wx.getStorageSync('siteUrl')
    app.location_attachurl(that)
    this.setData({
      siteUrl: siteUrl,
    })
    this.getyx()
    this.once()
    
  },

  tab:function(e){
    this.setData({
      activeNav: e.currentTarget.dataset.tabindex
    })
    var activeNav = this.data.activeNav
    if (activeNav==0){
      this.getyx()
    } else if (activeNav==1){
      this.getAll()
    }
  },

  // 推荐数据获取
  getyx:function(e){
    var that=this
    var show = false
    var hide=true
    app.util.request({
      url:'entry/wxapp/Storeup',
      success:function(res){
        
        if(res.data.code==500){
          that.setData({
            noData:show
          })
        }else if(res.data.code==200){
          var array = res.data.res
          for(var i=0;i<array.length;i++){
            array[i].img = array[i].img.split(',')
          }
          
          // array.img=array.img.split(',')
         that.setData({
           noData: hide,
           lists: array
         })
        }
      }
    })
  },


  // 全部数据获取
  getAll: function (e) {
    var that = this
    var show = false
    var hide = true
    app.util.request({
      url: 'entry/wxapp/Storeall',
      success: function (res) {
        
        if (res.data.code == 500) {
          that.setData({
            noData: show
          })
        } else if (res.data.code == 200) {
          var array = res.data.res
          for (var i = 0; i < array.length; i++) {
            array[i].img = array[i].img.split(',')
          }
          
          // array.img=array.img.split(',')
          that.setData({
            noData: hide,
            lists: array
          })
        }
      }
    })
  },


  // 地摊分类
  once:function(e){
    var that=this
    app.util.request({
      url: 'entry/wxapp/catestopdw',
      success: function (res) {
        
        var array = res.data.res
        // that.setData({
        //   list: array
        // })
        var typelist = [];
        for (let i = 0; i <array.length; i++) {
          if (array[i].arr.length > 0) {
            typelist.push(array[i])
          }
        }
        var indexheight = 0
        for (var i = 0; i < typelist.length; i++) {
          indexheight += (Math.floor(typelist[i].arr.length / 3) + 1) * 100;
          dataheith.push(indexheight);
        }
        //
        let { window_height, tab_config } = that.data;
        // var sjxx = wx.getSystemInfoSync()
        wx.getSystemInfo({
          success: function (res) {
            that.setData({
              // tab_config: {
              //   tabs: array,
              // },
              windowHeight: res.windowHeight
            })
          }
        })
        window_height = that.data.windowHeight - 92

        // tab_config.item_height = window_height / tab_config.tabs.length;
        tab_config.item_height = 50

        that.setData({ "window_height": window_height, "tab_config": tab_config });
        that.setData({
          tab_config: {
            tabs: typelist,
            tab_left: 0,
          },
        })
      }
      
    })
  },
  

  catItemClick: function (e) {
    var that = this;
    var activeIndex = that.data.activeIndex
    // that.updateSelectedPage(parseInt(activeIndex));
    this.setData({
      intoid: "id" + e.currentTarget.dataset.index,
      activeIndex: e.currentTarget.dataset.index,
      scroll: ""
    });
    setTimeout(function () {
      that.setData({
        scroll: "scroll"
      });
    }, 500)
  },

  // 滚动触发
  scroll: function (e) {
    var that = this;
    var scrolltop = e.detail.scrollTop;
    var scrollbotton = e.detail.scrollTop
    for (var i = 0; i < dataheith.length; i++) {
      if (scrolltop <= dataheith[i - 1]) {
        if (activeIndex != i) {
          that.updateSelectedPage(i - 2);
          activeIndex = i - 2
          if (activeIndex == -1) {
            this.setData({
              activeIndex: 0
            })
            return
          }
          this.setData({
            activeIndex: activeIndex
          })
        }
        break
      }
    }
  },


  // 更换页面到指定page ，从0开始
  updateSelectedPage(page) {
    let that = this;
    let { window_height, tab_config } = this.data;
    let underline_offset = tab_config.item_width * page;

    let show_item_num = Math.round(window_height / tab_config.item_height);
    let min_left_item = tab_config.item_height * (page - show_item_num + 1);
    let max_left_item = tab_config.item_height * page;
    if (tab_config.tab_left < min_left_item || tab_config.tab_left > max_left_item) {
      tab_config.tab_left = max_left_item - (window_height - tab_config.item_height) / 2;
    }
    that.setData({
      "tab_config": tab_config,
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
    var activeNav = that.data.activeNav
    if (activeNav == 0) {
      that.getyx()
    } else if (activeNav == 1){
      that.getAll()
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this
    var activeNav = that.data.activeNav
    if (activeNav==0){
      that.getyx()
    } else if (activeNav == 1) {
      that.getAll()
    }
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that=this
    that.data.page = that.data.page + 1
    var activeNav = that.data.activeNav
    if (activeNav==0){
      app.util.request({
        'url': 'entry/wxapp/Storeup',
        'cachetime': '0',
        data: { page: that.data.page },
        success: function (res) {
          
          if (res.data.code == 500) {
            wx.showToast({
              title: '没有新的数据了！',
              icon: 'none'
            })
          } else if (res.data.code == 200) {
              for (let j in res.data.res) {
                res.data.res[j].img = res.data.res[j].img.split(',')
              }
              for (var i = 0; i < res.data.res.length; i++) {
                that.data.lists.push(res.data.res[i])
              }
              that.setData({
                lists: that.data.lists
              })
              
          }
        }
      })
    }else if (activeNav == 1) {
      app.util.request({
        'url': 'entry/wxapp/Storeall',
        'cachetime': '0',
        data: { page: that.data.page },
        success: function (res) {
          
          if (res.data.code == 500) {
            wx.showToast({
              title: '没有新的数据了！',
              icon: 'none'
            })
          } else if (res.data.code == 200) {
            for (let j in res.data.res) {
              res.data.res[j].img = res.data.res[j].img.split(',')
            }
            for (var i = 0; i < res.data.res.length; i++) {
              that.data.lists.push(res.data.res[i])
            }
            that.setData({
              lists: that.data.lists
            })
            
          }
        }
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})